﻿using Newtonsoft.Json;
using System.Text;

namespace WxApi.Comm
{
    public static class HttpComm
    {
        public static async Task<string> GetRequest(string uri)
        {
            using (HttpClient client = new HttpClient())
            {
                return await client.GetStringAsync(uri);
            }
        }


        public static async Task<string> PostRequest(string uri, HttpContent content)
        {
            using (HttpClient client = new HttpClient())
            {
                var response = await client.PostAsync(uri, content);
                response.EnsureSuccessStatusCode();
                return await response.Content.ReadAsStringAsync();
            }
        }

        public static async Task<string> PostJson(string uri, object data)
        {
            using (HttpClient client = new HttpClient())
            {
                var json = JsonConvert.SerializeObject(data);
                var content = new StringContent(json, Encoding.UTF8, "application/json");
                var response = await client.PostAsync(uri, content);
                response.EnsureSuccessStatusCode();
                return await response.Content.ReadAsStringAsync();
            }
        }
    }
}
